package com.frank.oj.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.frank.model.entity.problem.Problem;
import com.frank.oj.common.exception.StatusFailException;
import com.frank.oj.common.result.CommonResult;
import com.frank.oj.common.result.ResultStatus;
import com.frank.oj.model.dto.ChangeGroupProblemProgressDTO;
import com.frank.oj.service.admin.problem.AdminGroupProblemService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author frank
 * @Date 2024/4/5
 */
@RestController
@RequestMapping("/api/admin/group-problem")
@RequiresAuthentication
@RequiresRoles(value = {"root", "problem_admin"}, logical = Logical.OR)
public class AdminGroupProblemController {

    @Resource
    private AdminGroupProblemService adminGroupProblemService;

    @GetMapping("/list")
    public CommonResult<IPage<Problem>> getProblemList(@RequestParam(value = "currentPage", defaultValue = "1") Integer currentPage,
                                                       @RequestParam(value = "limit", defaultValue = "10") Integer limit,
                                                       @RequestParam(value = "keyword", required = false) String keyword,
                                                       @RequestParam(value = "gid", required = false) Long gid) {
        return CommonResult.successResponse(adminGroupProblemService.getProblemList(currentPage, limit, keyword, gid));
    }

    /**
     * 处理团队题目的进度
     * @param changeGroupProblemProgressDto
     * @return
     */
    @PutMapping("/change-progress")
    public CommonResult<Void> changeProgress(@RequestBody ChangeGroupProblemProgressDTO changeGroupProblemProgressDto) {
        try {
            adminGroupProblemService.changeProgress(changeGroupProblemProgressDto);
            return CommonResult.successResponse();
        } catch (StatusFailException e) {
            return CommonResult.errorResponse(e.getMessage(), ResultStatus.FAIL);
        }
    }
}
